package cn.anecansaitin.whimsy.common.extension.comboSystem;

public interface IChargeable {
    default boolean isCharging(ExtraData extra) {
        Charge c = extra.getData("charging", Charge.class).orElseGet(() -> {
            Charge charge = new Charge(0, false);
            extra.putData("charging", charge);
            return charge;
        });

        return c.charged && !c.cancel;
    }

    default void setCharging(ExtraData extra, boolean value) {
        extra.getData("charging", Charge.class).ifPresentOrElse(c -> c.charged = value, () -> extra.putData("charging", new Charge(0, value)));
    }

    default void setChargingCanceled(ExtraData extra) {
        extra.getData("charging", Charge.class).ifPresentOrElse(c -> c.cancel = true, () -> {
            Charge charge = new Charge(0, false);
            charge.cancel = true;
            extra.putData("charging", charge);
        });
    }

    default int getChargedTick(ExtraData extra) {
        return extra.getData("charging", Charge.class).orElseGet(() -> {
            Charge charge = new Charge(0, false);
            extra.putData("charging", charge);
            return charge;
        }).tick;
    }

    default void addChargedTick(ExtraData extra) {
        extra.getData("charging", Charge.class).ifPresentOrElse(c -> c.tick++, () -> extra.putData("charging", new Charge(1, false)));
    }

    default void removeCache(ExtraData extra) {
        extra.removeData("charging");
    }

    class Charge {
        public int tick;
        public boolean charged;
        public boolean cancel;

        public Charge(int tick, boolean charged) {
            this.tick = tick;
            this.charged = charged;
        }
    }
}
